#include "weather.h"
#include "ui_weather.h"

extern QStringList list;

weather::weather(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::weather)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_DeleteOnClose);

    QFile file(":/music.qss"); // 假设style.qss位于资源文件中
    if (file.open(QFile::ReadOnly))
    {
        setStyleSheet(QString(file.readAll())); // 应用全局样式
        file.close();
    }

    setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
    setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明

    manager = new QNetworkAccessManager(this);
    connect(manager, &QNetworkAccessManager::finished,
            this, &weather::replyFinished);

    manager->get(QNetworkRequest(QUrl("http://t.weather.sojson.com/api/weather/city/101010100")));

    model = new QStandardItemModel(2,7,ui->weathertableView);
    ui->weathertableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->weathertableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->weathertableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->weathertableView->setFocusPolicy(Qt::NoFocus);
    ui->weathertableView->horizontalHeader()->setStyleSheet("QHeaderView::section{background: white; border: 0.5px solid #E0E0E0;}");
    ui->weathertableView->setShowGrid(false);
    ui->weathertableView->setStyleSheet("QTableView::Item{border:0px solid rgb(255,0,0);"
                                        "border-bottom:1px solid rgb(255,0,0);}");


    mS = new myStyledItemDelegate(ui->weathertableView);
    ui->weathertableView->setItemDelegate(mS);

    ui->citylineEdit->setEnabled(false);

//    connect(this, &weather::open, this->parent, &mainInterface::openInterface);
}

weather::~weather()
{
    delete ui;
}

void weather::replyFinished(QNetworkReply *reply)
{
    if(200 == reply->attribute(QNetworkRequest::HttpStatusCodeAttribute))
    {
        analysis(reply->readAll());
    }
}

void weather::analysis(QByteArray byte)
{
    QJsonDocument doc = QJsonDocument::fromJson(byte);
    QJsonObject object;
    QJsonArray array;

    QString ymd,high,low,week,type;

    if(true == doc.isObject())
    {
        object = doc.object();
        if(true == object.contains("cityInfo") && true == object.value("cityInfo").isObject())
        {
            QJsonObject o = object.value("cityInfo").toObject();
            if(true == o.contains("city") && true == o.value("city").isString())
            {
                city = o.value("city").toString();
            }
            if(true == o.contains("parent") && true == o.value("parent").isString())
            {
                parent = o.value("parent").toString();
            }
        }
        if(true == object.contains("data") && true == object.value("data").isObject())
        {
            QJsonObject o = object.value("data").toObject();
            if(true == o.contains("forecast") && true == o.value("forecast").isArray())
            {
                array = o.value("forecast").toArray();
                size = array.size();
                QStringList list;
                for(int i = 1; i < size && true == array[i].isObject(); i++)
                {
                    o = array[i].toObject();
                    if(true == o.contains("ymd") && true == o.value("ymd").isString())
                        ymd = o.value("ymd").toString();
                    if(true == o.contains("high") && true == o.value("high").isString())
                    {
                        QStringList list = o.value("high").toString().split(' ');
                        high = "最高温度 " + list[1];
                    }
                    if(true == o.contains("low") && true == o.value("low").isString())
                    {
                        QStringList list = o.value("low").toString().split(' ');
                        low = "最低温度 " + list[1];
                    }
                    if(true == o.contains("week") && true == o.value("week").isString())
                        week = o.value("week").toString();
                    if(true == o.contains("type") && true == o.value("type").isString())
                        type = o.value("type").toString();

                    QString str = high; str += "  "; str += low;
                    str += "  "; str += type; str += "  "; str += ymd;
                    qDebug()<<str;
                    QStandardItem * item = new QStandardItem(str);
                    item->setTextAlignment(Qt::AlignCenter);

                    if(7 >= i)
                    {
                        model->setItem(0, i - 1, item);
                        list << week;
                    }
                    else
                        model->setItem(1, i - 7 - 1, item);
                    //qDebug()<<date<<":"<<parent<<city<<high<<low<<week<<type<<'\n';
                }

                model->setHorizontalHeaderLabels(list);//设置数据的表头
            }
        }
    }
    ui->weathertableView->setModel(model);
    ui->citylineEdit->setText(parent + ' ' + city);
}

void weather::mousePressEvent(QMouseEvent *event)
{
    if(false == ui->closeButton->underMouse())
        mouseOff = event->globalPos() - geometry().topLeft();
}

void weather::mouseMoveEvent(QMouseEvent *event)
{
    if(false == ui->closeButton->underMouse())
        this->move(event->globalPos() - mouseOff);
}

void weather::on_closeButton_clicked()
{
    emit open();
    this->close();
}

void weather::on_searchPushButton_clicked()
{
    QString code = "";
    qDebug()<<ui->lineEdit->text()<<::list.size();
    for(int i = 0; i < ::list.size(); i++)
    {
        QStringList city = ::list[i].split('\t');
        qDebug()<<"1:"<<city[1];
        if(ui->lineEdit->text() == city[1])
        {
            code = city[0];
            qDebug()<<"code:"<<code;
            break;
        }
    }
    if("" == code)
        QToolTip::showText(ui->lineEdit->mapToGlobal(QPoint(25, 15)), "地域输入有误，请重新输入！");
    else
        manager->get(QNetworkRequest(QUrl(QString("http://t.weather.sojson.com/api/weather/city/%1").arg(code))));

}
